{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #寻找数组的中心下标"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #prefix-sum"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #前缀和"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: pivotIndex"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #寻找数组的中心下标"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组&nbsp;<code>nums</code> ，请计算数组的 <strong>中心下标 </strong>。</p>\n",
    "\n",
    "<p>数组<strong> 中心下标</strong><strong> </strong>是数组的一个下标，其左侧所有元素相加的和等于右侧所有元素相加的和。</p>\n",
    "\n",
    "<p>如果中心下标位于数组最左端，那么左侧数之和视为 <code>0</code> ，因为在下标的左侧不存在元素。这一点对于中心下标位于数组最右端同样适用。</p>\n",
    "\n",
    "<p>如果数组有多个中心下标，应该返回 <strong>最靠近左边</strong> 的那一个。如果数组不存在中心下标，返回 <code>-1</code> 。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,7,3,6,5,6]\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：</strong>\n",
    "中心下标是 3 。\n",
    "左侧数之和 sum = nums[0] + nums[1] + nums[2] = 1 + 7 + 3 = 11 ，\n",
    "右侧数之和 sum = nums[4] + nums[5] = 5 + 6 = 11 ，二者相等。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1, 2, 3]\n",
    "<strong>输出：</strong>-1\n",
    "<strong>解释：</strong>\n",
    "数组中不存在满足此条件的中心下标。</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [2, 1, -1]\n",
    "<strong>输出：</strong>0\n",
    "<strong>解释：</strong>\n",
    "中心下标是 0 。\n",
    "左侧数之和 sum = 0 ，（下标 0 左侧不存在元素），\n",
    "右侧数之和 sum = nums[1] + nums[2] = 1 + -1 = 0 。</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= nums.length &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>-1000 &lt;= nums[i] &lt;= 1000</code></li>\n",
    "</ul>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><meta charset=\"UTF-8\" />注意：本题与主站 724&nbsp;题相同：&nbsp;<a href=\"https://leetcode-cn.com/problems/find-pivot-index/\">https://leetcode-cn.com/problems/find-pivot-index/</a></p>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [tvdfij](https://leetcode.cn/problems/tvdfij/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [tvdfij](https://leetcode.cn/problems/tvdfij/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,7,3,6,5,6]', '[1,2,3]', '[2,1,-1]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        r=sum(nums)\n",
    "        l=0\n",
    "        for i,num in enumerate(nums):\n",
    "            r-=num\n",
    "            if r==l:\n",
    "                return i\n",
    "            l+=num  \n",
    "        return -1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        sumn = sum(nums)\n",
    "        for i in range(len(nums)):\n",
    "            if i == len(nums)-1:\n",
    "                sumi = 0\n",
    "            else:\n",
    "                sumi = sum(nums[i+1:])\n",
    "            if ((sumn-nums[i]) %2 == 0) and (sumi == (sumn-nums[i]) // 2):\n",
    "                return i\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        l = 0\n",
    "        r = sum(nums[1:])\n",
    "        for m in range(len(nums)):\n",
    "            if l == r:\n",
    "                return m\n",
    "            elif m < len(nums)-1:\n",
    "                l += nums[m]\n",
    "                r -= nums[m+1]\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total=sum(nums)\n",
    "        left=0\n",
    "        right=total-nums[0]\n",
    "        index=0\n",
    "        n=len(nums)\n",
    "        while index<n-1:\n",
    "            if left==right:\n",
    "                return index\n",
    "            left+=nums[index]\n",
    "            index+=1\n",
    "            right-=nums[index]\n",
    "        if left==right:\n",
    "            return index\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        left, right = 0, sum(nums[1:])\n",
    "        if left == right:\n",
    "            return 0\n",
    "        for i in range(1, len(nums)):\n",
    "            left += nums[i-1]\n",
    "            right -= nums[i]\n",
    "            if left == right:\n",
    "                return i\n",
    "\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        \"\"\"\n",
    "        :type nums: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        if len(nums) < 1:\n",
    "            return -1\n",
    "        \n",
    "        for i in range(1, len(nums)):\n",
    "            nums[i] = nums[i-1] + nums[i]\n",
    "            \n",
    "        ans = -1\n",
    "        \n",
    "        for i in range(0, len(nums)):\n",
    "            prev = 0 if i == 0 else nums[i-1]\n",
    "            suff = 0 if i == len(nums)-1 else nums[-1] - nums[i]\n",
    "            if prev == suff:\n",
    "                ans = i\n",
    "                break\n",
    "                \n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "\n",
    "        s = sum(nums)\n",
    "        n = len(nums)\n",
    "\n",
    "        k = 0\n",
    "        for i in range(n):\n",
    "            if k * 2 + nums[i] == s:\n",
    "                return i\n",
    "            k += nums[i]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        left_sum = 0\n",
    "        right_sum = sum(nums)\n",
    "\n",
    "        for i in range(len(nums)):\n",
    "            right_sum -= nums[i]\n",
    "\n",
    "            if left_sum == right_sum:\n",
    "                return i\n",
    "        \n",
    "            left_sum += nums[i]\n",
    "        \n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total=sum(nums)\n",
    "        sum_=0\n",
    "        for i in range(len(nums)):\n",
    "            if sum_*2+nums[i]==total:\n",
    "                return i \n",
    "            sum_+=nums[i]\n",
    "        return -1\n",
    "'''class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total = sum(nums)\n",
    "        left = 0\n",
    "        for i in range(len(nums)):\n",
    "            if left * 2 + nums[i] == total:\n",
    "                return i\n",
    "            left += nums[i]\n",
    "        return -1\n",
    "            '''\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        for i in range(len(nums)):\n",
    "            l=r=0\n",
    "            l+=sum(nums[:i])\n",
    "            r+=sum(nums[i+1:])\n",
    "            if l==r:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: list[int]) -> int:\n",
    "        s = sum(nums)\n",
    "        left = 0\n",
    "        right = 0\n",
    "        for i, v in enumerate(nums):\n",
    "            if i > 0:\n",
    "                left += nums[i - 1]\n",
    "            right = s - v - left\n",
    "            if left == right:\n",
    "                return i\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total = sum(nums)\n",
    "        pre_sum = 0 \n",
    "        for i in range(len(nums)):\n",
    "            if pre_sum*2 + nums[i] == total:\n",
    "                return i\n",
    "            pre_sum += nums[i]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        numsum=sum(nums)\n",
    "        k=0\n",
    "        for i in range(len(nums)):\n",
    "            if k*2+nums[i]==numsum:\n",
    "                return i\n",
    "                break\n",
    "            k=k+nums[i]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        left_sum=0\n",
    "        right_sum=sum(nums[1:])\n",
    "        n=len(nums)\n",
    "        for i in range(0,n):\n",
    "            if left_sum==right_sum:\n",
    "                return i\n",
    "            left_sum+=nums[i]\n",
    "            right_sum-=nums[i+1] if i!=n-1 else 0\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total = sum(nums)\n",
    "        pre_sum = 0 \n",
    "        for i in range(len(nums)):\n",
    "            if pre_sum*2 + nums[i] == total:\n",
    "                return i\n",
    "            pre_sum += nums[i]\n",
    "        return -1\n",
    "\n",
    "# 作者：int_64\n",
    "# 链接：https://leetcode.cn/problems/tvdfij/solutions/1402823/by-jam007-cc15/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        n=len(nums)\n",
    "        nums=nums+[0]\n",
    "        m=sum(nums)-nums[0]\n",
    "        q=0\n",
    "        for i in range(n):\n",
    "            if q==m:\n",
    "                return i\n",
    "            q+=nums[i]\n",
    "            m-=nums[i+1]\n",
    "            \n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        for i in range(len(nums)):\n",
    "            if sum(nums[0:i]) == sum(nums[i+1:]):\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total = sum(nums)\n",
    "        pre_sum = 0 \n",
    "        for i in range(len(nums)):\n",
    "            if pre_sum*2 + nums[i] == total:\n",
    "                return i\n",
    "            pre_sum += nums[i]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        # 方式一\n",
    "        # for i in range(len(nums)):\n",
    "        #     if sum(nums[0:i]) == sum(nums[i+1:]):\n",
    "        #         return i\n",
    "        # return -1\n",
    "\n",
    "        # 方式二\n",
    "        # total = sum(nums)\n",
    "        # for i in range(len(nums)):\n",
    "        #     x = sum(nums[0:i])\n",
    "        #     if  x == total - x -nums[i]:\n",
    "        #         return i\n",
    "        # return -1\n",
    "\n",
    "        # 方式三\n",
    "        total = sum(nums)\n",
    "        pre_sum = 0 \n",
    "        for i in range(len(nums)):\n",
    "            if pre_sum*2 + nums[i] == total:\n",
    "                return i\n",
    "            pre_sum += nums[i]\n",
    "        return -1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            if sum(nums[:i]) == sum(nums[i+1:]):\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        # solution 1\n",
    "        for i in range(len(nums)-1):\n",
    "            if sum(nums[:i])==sum(nums[i+1:]):\n",
    "                return i\n",
    "        if sum(nums[:-1])==0:\n",
    "            return len(nums)-1\n",
    "        return -1\n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        sum = 0\n",
    "        for i in nums:\n",
    "            sum += i\n",
    "        \n",
    "        resl = 0\n",
    "        \n",
    "        if sum - nums[0] == 0:\n",
    "            return 0\n",
    "        \n",
    "        for i in range(1,len(nums)):\n",
    "            resl += nums[i - 1]\n",
    "            if resl * 2 == sum - nums[i]:\n",
    "                return i\n",
    "            \n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "    \n",
    "        # 前缀和\n",
    "        pre_sums=[]\n",
    "        sum=0\n",
    "        for num in nums:\n",
    "            sum+=num\n",
    "            pre_sums.append(sum)\n",
    "        \n",
    "        result=-1\n",
    "        total=pre_sums[-1]\n",
    "        # 从i+1开始算\n",
    "        \n",
    "        if 0==total-pre_sums[0]:\n",
    "            return 0\n",
    "        result=-1\n",
    "        for i in range(1,len(nums)):\n",
    "            if total-pre_sums[i]== pre_sums[i-1]:\n",
    "                return i\n",
    "        return result\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total = sum(nums)\n",
    "        cur_sum = 0\n",
    "        for i in range(len(nums)):\n",
    "            # cur_sum += nums[i]\n",
    "            # if cur_sum == total:\n",
    "            #     return i\n",
    "            # total -= nums[i]\n",
    "            if 2*cur_sum+nums[i] == total:\n",
    "                return i\n",
    "            cur_sum += nums[i]\n",
    "\n",
    "        return -1\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        sums,sumv = sum(nums),0\n",
    "        if sums==nums[0]:return 0\n",
    "        for i in range(1,len(nums)):\n",
    "            sumv+=nums[i-1]\n",
    "            if sumv*2==sums-nums[i]:return i\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        # 方式一\n",
    "        # for i in range(len(nums)):\n",
    "        #     if sum(nums[0:i]) == sum(nums[i+1:]):\n",
    "        #         return i\n",
    "        # return -1\n",
    "\n",
    "        # 方式二\n",
    "        # total = sum(nums)\n",
    "        # for i in range(len(nums)):\n",
    "        #     x = sum(nums[0:i])\n",
    "        #     if  x == total - x -nums[i]:\n",
    "        #         return i\n",
    "        # return -1\n",
    "\n",
    "        # 方式三\n",
    "        total = sum(nums)\n",
    "        pre_sum = 0 \n",
    "        for i in range(len(nums)):\n",
    "            if pre_sum*2 + nums[i] == total:\n",
    "                return i\n",
    "            pre_sum += nums[i]\n",
    "        return -1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total = sum(nums)\n",
    "        left = 0\n",
    "        for i in range(len(nums)):\n",
    "            if left * 2 + nums[i] == total:\n",
    "                return i\n",
    "            left += nums[i]\n",
    "        return -1\n",
    "            \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total = sum(nums)\n",
    "        pre_sum = 0 \n",
    "        for i in range(len(nums)):\n",
    "            if pre_sum*2 + nums[i] == total:\n",
    "                return i\n",
    "            pre_sum += nums[i]\n",
    "        return -1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total = sum(nums)\n",
    "        sum_ = 0\n",
    "        n = len(nums)\n",
    "\n",
    "        \n",
    "        for i in range(n):\n",
    "            if total  - nums[i] - sum_ == sum_:\n",
    "                return i\n",
    "            sum_ += nums[i]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        Sum = sum(nums)\n",
    "        presum = 0\n",
    "        for i, num in enumerate(nums):\n",
    "            if presum*2+num == Sum:\n",
    "                return i\n",
    "            presum += num\n",
    "        return -1\n",
    "\n",
    "\n",
    "        \n",
    "            \n",
    "\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: list[int]) -> int:\n",
    "        s = sum(nums)\n",
    "        left = 0\n",
    "        right = 0\n",
    "        for i, v in enumerate(nums):\n",
    "            if i > 0:\n",
    "                left += nums[i - 1]\n",
    "            right = s - v - left\n",
    "            if left == right:\n",
    "                return i\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        prefix, total = 0, sum(nums)\n",
    "        for i in range(len(nums)):\n",
    "            if prefix * 2 + nums[i] == total:\n",
    "                return i\n",
    "\n",
    "            prefix += nums[i]\n",
    "\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total = sum(nums)\n",
    "        left = 0\n",
    "        for i, val in enumerate(nums):\n",
    "            if left*2+val == total:\n",
    "                return i\n",
    "            left += val\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        for i in range(len(nums)):\n",
    "            if sum(nums[:i])==sum(nums[i+1:]):\n",
    "                return i\n",
    "                break\n",
    "        else:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        l,r = 0,sum(nums)\n",
    "        for i in range(len(nums)):\n",
    "            r -= nums[i]\n",
    "            if l == r:\n",
    "                return i \n",
    "            l += nums[i]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        l = 0\n",
    "        r = sum(nums) - nums[0]\n",
    "        n = len(nums)\n",
    "        if l==r:\n",
    "            return 0\n",
    "        else:\n",
    "            for i in range(1, n):\n",
    "                l += nums[i-1]\n",
    "                r -= nums[i]\n",
    "                if l==r:\n",
    "                    return i\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        sm = sum(nums)\n",
    "        ln = len(nums)\n",
    "        lsm = 0\n",
    "        rsm = sm\n",
    "        for i in range(ln):\n",
    "            rsm -= nums[i]\n",
    "            if rsm == sm - rsm - nums[i]:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        ans=-1\n",
    "        s=sum(nums)\n",
    "        t=0\n",
    "        if s-nums[0]==t:\n",
    "            return 0\n",
    "        for i in range(len(nums)-1):\n",
    "            t+=nums[i]\n",
    "            if t==(s-nums[i+1])/2:\n",
    "                ans=i+1\n",
    "                break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        leftnum = 0\n",
    "        s = 0\n",
    "        for i in nums:\n",
    "            s += i\n",
    "        for i in range(len(nums)):\n",
    "            if leftnum == (s - nums[i] - leftnum):\n",
    "                return i\n",
    "            leftnum += nums[i]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        sum_value = sum(nums)\n",
    "        now_value = 0\n",
    "        for center in range(len(nums)):\n",
    "            if now_value == (sum_value - nums[center])/2:\n",
    "                return center\n",
    "            else:\n",
    "                now_value += nums[center]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        l,r = 0,sum(nums)\n",
    "        for i,v in enumerate(nums):\n",
    "            r -= v\n",
    "            if l == r:\n",
    "                return i \n",
    "            l += v\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total=sum(nums)\n",
    "        sum_=0\n",
    "        for i in range(len(nums)):\n",
    "            if 2*sum_+nums[i]==total:\n",
    "                return i \n",
    "            sum_+=nums[i]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        s = sum(nums)\n",
    "        pre_sum = 0\n",
    "        res = -1\n",
    "        for i in range(len(nums)):\n",
    "            if pre_sum == (s - nums[i]) / 2:\n",
    "                res = i\n",
    "                break\n",
    "            pre_sum += nums[i]\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        nums_sum = sum(nums)\n",
    "        pre_sum = 0\n",
    "        for idx, num in enumerate(nums):\n",
    "            tgt_sum = nums_sum-pre_sum-num\n",
    "            if pre_sum==tgt_sum:\n",
    "                return idx\n",
    "            pre_sum += num\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total = sum(nums)\n",
    "        for k, v in enumerate(nums):\n",
    "            # 妙：左边和乘以2，加上当前值，就是整个列表和\n",
    "            if sum(nums[0:k]) * 2 + nums[k] == total:\n",
    "                return k\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        res=0\n",
    "        n=len(nums)\n",
    "        for i in range(n):\n",
    "            if i==0 or i==n-1:\n",
    "                sum_=0\n",
    "            if sum(nums[0:i])==sum(nums[i+1:]):\n",
    "                return i\n",
    "        \n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import List\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total = sum(nums)\n",
    "        for i in range(len(nums)):\n",
    "            if sum(nums[:i]) == (total - nums[i]) / 2.0:\n",
    "                print('nums[:i]:', nums[:i])\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total = sum(nums)\n",
    "        pre_sum = 0\n",
    "        # 左侧元素和 + 此元素 = 总体元素和 - 左侧元素和\n",
    "        for i in range(len(nums)):\n",
    "            if pre_sum * 2 + nums[i] == total:\n",
    "                return i \n",
    "            pre_sum += nums[i]\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        # solution 1\n",
    "        '''\n",
    "        for i in range(len(nums)-1):\n",
    "            if sum(nums[:i])==sum(nums[i+1:]):\n",
    "                return i\n",
    "        if sum(nums[:-1])==0:\n",
    "            return len(nums)-1\n",
    "        return -1\n",
    "        '''\n",
    "        # solution 2\n",
    "        left_num,right_num,n=0,sum(nums),len(nums)\n",
    "        for i in range(n):\n",
    "            right_num-=nums[i]\n",
    "            if right_num==left_num:\n",
    "                return i \n",
    "            left_num+=nums[i]\n",
    "        return -1\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        x = sum(nums)\n",
    "        an = 0\n",
    "        tu = 0\n",
    "        for i in range(len(nums)):\n",
    "            t = round((x-nums[i])/2, 2)\n",
    "            if an == t:\n",
    "                tu = 1\n",
    "                break\n",
    "            an += nums[i]\n",
    "        return -1 if tu == 0 else i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        num_list = [0]*len(nums)\n",
    "        for i in range(len(nums)):\n",
    "            num_list[i] = nums[i]\n",
    "            if i >0:\n",
    "                num_list[i] += num_list[i-1]\n",
    "        if num_list[-1]-num_list[0]==0:\n",
    "            return 0\n",
    "        for i in range(1,len(num_list)):\n",
    "            if num_list[-1]-num_list[i] == num_list[i-1]:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        for i in range(n):\n",
    "            right = sum(nums[:i])\n",
    "            left = sum(nums[i+1:])\n",
    "            if left == right:\n",
    "                return i\n",
    "\n",
    "        \n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        if not nums:\n",
    "            return -1\n",
    "        n = len(nums)\n",
    "        if n == 1:\n",
    "            return -1\n",
    "        l, r = 0, sum(nums)-nums[0]\n",
    "        for i in range(1, n):\n",
    "            if l == r:\n",
    "                return i-1\n",
    "            l += nums[i-1]\n",
    "            r -= nums[i]\n",
    "        return -1 if l!=r else n-1\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        # 方式一\n",
    "        # for i in range(len(nums)):\n",
    "        #     if sum(nums[0:i]) == sum(nums[i+1:]):\n",
    "        #         return i\n",
    "        # return -1\n",
    "\n",
    "        # 方式二\n",
    "        total = sum(nums)\n",
    "        for i in range(len(nums)):\n",
    "            x = sum(nums[0:i])\n",
    "            if  x == total - x -nums[i]:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        if not nums:\n",
    "            return -1\n",
    "        # 计算nums的和\n",
    "        the_sum = sum(nums)\n",
    "        # 初始化变量，用于存储前面部分的和\n",
    "        pre_sum = 0\n",
    "        for i in range(len(nums)):\n",
    "            if pre_sum*2+nums[i]==the_sum:\n",
    "                return i\n",
    "            pre_sum += nums[i]\n",
    "        \n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        n_len = len(nums)\n",
    "        total_sum = sum(nums)\n",
    "        left_sum = 0\n",
    "        for i in range(n_len):\n",
    "            # 总和减去当前位置的结果的一半是否等于左边和\n",
    "            # 减少右边计算量\n",
    "            if left_sum == (total_sum - nums[i])/2:\n",
    "                return i\n",
    "            else:\n",
    "                left_sum += nums[i]\n",
    "\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        l = 0\n",
    "        r = sum(nums)-nums[0]\n",
    "        k = 0\n",
    "        if l == r:\n",
    "            return k\n",
    "        else:\n",
    "            while k<len(nums)-1 and l != r:\n",
    "                k = k + 1\n",
    "                l = l + nums[k-1]\n",
    "                r=r-nums[k]\n",
    "            if l == r:\n",
    "                return k\n",
    "            else:\n",
    "                return -1\n",
    "          "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        running_sum = [0] * n\n",
    "        running_sum[0] = nums[0]\n",
    "        for i in range(1, n):\n",
    "            running_sum[i] = running_sum[i - 1] + nums[i]\n",
    "        for i in range(n):\n",
    "            left_sum = 0\n",
    "            if i > 0:\n",
    "                left_sum = running_sum[i - 1]\n",
    "            right_sum = 0\n",
    "            if i < n - 1:\n",
    "                right_sum = running_sum[n - 1] - running_sum[i]\n",
    "            if left_sum == right_sum:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total=sum(nums)\n",
    "        n=len(nums)\n",
    "        left,right=0,0\n",
    "        sumleft=0\n",
    "        for i in range(n):\n",
    "            sumleft+=nums[i]\n",
    "            sumright=total-sumleft\n",
    "            if sumleft-nums[i]==sumright:\n",
    "                return i\n",
    "        return -1\n",
    "            \n",
    "       "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total = sum(nums)\n",
    "        for k, v in enumerate(nums):\n",
    "            # 妙：左边和乘以2，加上当前值，就是整个列表和\n",
    "            if sum(nums[:k]) * 2 + nums[k] == total:\n",
    "                return k\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        pre = 0\n",
    "        total = sum(nums)\n",
    "        for x, n in enumerate(nums):\n",
    "            if pre * 2 + n == total:\n",
    "                return x\n",
    "            pre += n\n",
    "        return -1\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        cur=0\n",
    "        ttl=sum(nums)\n",
    "        for i in range(len(nums)):\n",
    "            if cur*2+nums[i]==ttl:\n",
    "                return i \n",
    "            cur += nums[i]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        count = sum(nums)\n",
    "        pre = 0\n",
    "        for i, num in enumerate(nums):\n",
    "            if (count - num) / 2 == pre:\n",
    "                return i\n",
    "            pre += num\n",
    "        return -1\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        stop = False\n",
    "        leftsum=0\n",
    "        rightsum=sum(nums)\n",
    "        key = -1\n",
    "        '''\n",
    "        if leftsum==rightsum:\n",
    "            stop=True\n",
    "            return 0\n",
    "            '''\n",
    "        while not stop:\n",
    "            for i in range(len(nums)):\n",
    "                print(leftsum,rightsum)\n",
    "                if i==0:\n",
    "                    leftsum=0\n",
    "                    rightsum -=nums[0]\n",
    "                    if leftsum == rightsum:\n",
    "                        stop=True\n",
    "                        key=i\n",
    "                        break\n",
    "                else:\n",
    "                    leftsum +=nums[i-1]\n",
    "                    rightsum -= nums[i]\n",
    "                    if leftsum==rightsum:\n",
    "                        stop=True\n",
    "                        key=i\n",
    "                        break\n",
    "            if stop == False:\n",
    "                break\n",
    "        return key\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        prefix = [nums[0]] * n\n",
    "        for i in range(1, n):\n",
    "            prefix[i] = prefix[i-1] + nums[i]\n",
    "        if prefix[n-1] - nums[0] == 0:\n",
    "            return 0\n",
    "        for i in range(1, n-1):\n",
    "            if prefix[i] - nums[i] == prefix[n-1] - prefix[i]:\n",
    "                return i\n",
    "        if n>=2 and prefix[n-2] == 0:\n",
    "            return n - 1\n",
    "        return -1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total = sum(nums)\n",
    "        cnt = 0\n",
    "        for i in range(len(nums)):\n",
    "            if cnt == total - nums[i] - cnt:\n",
    "                return i\n",
    "            cnt += nums[i]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        sum_arr = sum(nums)\n",
    "        total = 0\n",
    "        for i, x in enumerate(nums):\n",
    "            total += x \n",
    "            if total == sum_arr:\n",
    "                return i\n",
    "            sum_arr -= x \n",
    "        return -1 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        total = sum(nums)\n",
    "        sums = 0\n",
    "        for i in range(n):\n",
    "            if sums == total - nums[i] - sums:\n",
    "                return i\n",
    "            sums += nums[i]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        l,r = 0,sum(nums)\n",
    "        for i in range(len(nums)):\n",
    "            r -= nums[i]\n",
    "            if l == r:\n",
    "                return i \n",
    "            l += nums[i]\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        lst, n = list(accumulate(nums, initial = 0)), len(nums)\n",
    "        for i, x, y in zip(range(n), nums, lst):\n",
    "            if lst[-1] - x == y << 1: return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        right = sum(nums)\n",
    "        left = 0\n",
    "        for i, x in enumerate(nums):\n",
    "            right -= x\n",
    "            if i > 0:\n",
    "                left += nums[i-1]\n",
    "            if left == right:\n",
    "                return i\n",
    "        if right == 0:\n",
    "            return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        ssum = sum(nums)\n",
    "        prefix = 0\n",
    "        for i in range(n):\n",
    "            if i > 0:\n",
    "                prefix += nums[i-1]\n",
    "            suffix = ssum - prefix - nums[i]\n",
    "            if prefix == suffix:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total = sum(nums)\n",
    "        pre = 0\n",
    "        for i, num in enumerate(nums):\n",
    "            if pre * 2 + num == total:\n",
    "                return i\n",
    "            pre += num\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total = sum(nums)\n",
    "        n = len(nums)\n",
    "        left = 0\n",
    "        right=0\n",
    "        # right=total-left\n",
    "\n",
    "        for i in range(n):\n",
    "            left += nums[i]\n",
    "            right = total - left\n",
    "            if left - nums[i] == right:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        sums = sum(nums) - nums[0]\n",
    "        previous = 0\n",
    "        found = False\n",
    "        for i in range(len(nums)):\n",
    "            if previous == sums:\n",
    "                ans = i\n",
    "                found = True\n",
    "                return i\n",
    "                break\n",
    "            previous += nums[i]\n",
    "            if i < len(nums)-1:\n",
    "                sums = sums- nums[i+1]\n",
    "        else:\n",
    "            if found == False:\n",
    "                return -1\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        total = 0\n",
    "        for i in nums:\n",
    "            total=total+i\n",
    "        sum=0\n",
    "        for i in range(len(nums)):\n",
    "            if (2*sum+nums[i]==total):\n",
    "                return i\n",
    "            sum=sum+nums[i]\n",
    "        return -1\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        lst, n = list(accumulate(nums, initial = 0)), len(nums)\n",
    "        for i, x, y in zip(range(n), nums, lst):\n",
    "            if lst[-1] - x == y << 1: return i\n",
    "        return -1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "\n",
    "        prefix_sums = []\n",
    "        sums = 0\n",
    "        for num in nums:\n",
    "            sums += num\n",
    "            prefix_sums.append(sums)\n",
    "\n",
    "        for i in range(n):\n",
    "            left = 0 if i == 0 else prefix_sums[i-1]\n",
    "            right = prefix_sums[-1] - prefix_sums[i]\n",
    "\n",
    "            if left == right:\n",
    "                return i\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def pivotIndex(self, arr: List[int]) -> int:\n",
    "        total=sum(arr)\n",
    "        to1=0\n",
    "        for i in range(len(arr)):\n",
    "            if to1==total-arr[i]-to1:\n",
    "                return i\n",
    "            to1+=arr[i]\n",
    "        return -1"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
